package specialfeatures;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;



import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


import org.apache.commons.lang3.StringEscapeUtils;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;



public class HttpXml {

	public static Document getRequest(String url1) throws IOException, ParserConfigurationException, SAXException, InterruptedException  {

		
		
		
		Document document=null;
		InputStream inputstream = null; 
		URL url=new URL(url1);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		
		try{
		DocumentBuilder builder = factory.newDocumentBuilder();
		inputstream = url.openStream();		
		
		document = builder.parse(inputstream);
		}catch (ParserConfigurationException parserconfigurationexception) {    
            throw parserconfigurationexception;  
        } catch (SAXException saxexception) {  
        	return null;   
        } catch (IOException ioexception) {  
            return null;  
        } finally{  
          if(inputstream != null){  
            inputstream.close();  
            //inputstream = null; 
            
          }  
          url = null;  
        }  
		
		return document;
		
	
	}
	public static String httpGet(String urlStr) throws IOException  {
		  URL url;
		  if (urlStr.contains("("))
			  return null;
		  else {
		try {
			url = new URL(urlStr);
		} catch (MalformedURLException e) {
		
			e.printStackTrace();
			return null;
		}
		  System.out.println(url);
		  String line="";
		  HttpURLConnection conn = null;
		  BufferedReader rd = null;
		  int code=0;
		try {
			conn = (HttpURLConnection) url.openConnection();
			code=conn.getResponseCode();
			 if (code== 200){
				  int append=0;
				  // Buffer the result into a string
				  rd = new BufferedReader(
				      new InputStreamReader(conn.getInputStream()));
				  Scanner sb = new Scanner(rd);
				  
				  String test=null;
				  while (sb.hasNext()) {
					  test=sb.nextLine();
					 if (test.contains("<p class=\"song\" id=\"songLyricsDiv_dex"))
				    {append=1;
				    
				    
				    }
					 else if (test.contains("<p id=\"songLyricsSmallDiv\" ondragstart=\"return false;")){
						 append=0;
						 
						 
						 }
					 else if (append==1){
						 if (test.contains("Sorry, we have no  -  lyrics at the moment")){
							 line= null;
							 break;
						 }
						 else if (test!=null)
						 line=line+test+"\n";
						
					 }
						 
				    }
				  rd.close();
				  conn.disconnect();
				  line= StringEscapeUtils.unescapeHtml4(line);
				  line= line.replaceAll("<br />", "");
				  line= line.replaceAll("</p>", "");
				  return line; 
				 
				  
				
				 
				  }
			 else if (code != 200) {
				   line= null;
				   System.out.println("The code is : " +code);
				  } 
				
		} catch (IOException e) {
			
			e.printStackTrace();
			System.out.println("this is a fiail!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
		}
	
		return line;
	}
	}	  
}
	
